<template>
  <div>
    <searchData :search-data="searchArr" @search="search" />
    <a-card :bordered="false">
      <!-- <div class="table-operator">
        <div class="pub-table-action-btn">
          <excel
            ref="exportSelect"
            file-name="调拨台账"
            is-click-generate="false"
          >
            <a-button
              slot="excelBtn"
              @click="exportSelect"
            >
              导出选中
            </a-button>
          </excel>
          <excel
            ref="exportAll"
            file-name="调拨台账"
            is-click-generate="false"
          >
            <a-button
              slot="excelBtn"
              @click="exportAll"
            >
              导出全部
            </a-button>
          </excel>
        </div>
        <div class="pub-table-select-count">
          <a-icon
            type="info-circle"
            theme="twoTone"
          />
          已选择{{ selectedRowKeys.length }}项
        </div>
      </div> -->
      <list-table
        ref="table"
        :columns="columns"
        :request="loadData"
        :start-index="0"
        :out-selected-row-keys="selectedRowKeys"
        :export-permission="{ select: '', all: '' }"
        :isHideFixed="true"
        :export-config="{
          use: false,
          select: $route.meta.title,
          all: $route.meta.title
        }"
      >
        <template slot="index" slot-scope="text, record, index">
          {{ index + 1 }}
        </template>
      </list-table>
      <!-- <pagination
        v-model="pageNo"
        :total="totalSize"
        :page-size.sync="pageSize"
        @change="handlePageChange"
        @showSizeChange="showSizeChange"
      /> -->
    </a-card>
  </div>
</template>
<script>
import { transfersList } from '@/api/material/remaining/parameter'
import SelectSubCompany from '@/components/Select/SelectSubCompany'
import SelectProject from '@/components/Select/SelectProject'
import OrgTreeSelect from '@/components/OrgTreeSelect'

export default {
  name: 'a' + Date.now(),
  data: function () {
    return {
      loading: false,
      queryParam: {},
      pageNo: 1,
      pageSize: 10,
      totalSize: 0,
      searchArr: [
        // 搜索框
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        // {
        //   name: '所属分公司',
        //   valueKey: 'branchCompanyId',
        //   type: SelectSubCompany
        // },
        // {
        //   name: '项目名称',
        //   type: SelectProject,
        //   valueKey: 'projectId'
        // },
        {
          name: '物资名称',
          value: 'materialName',
          type: 'input'
        },
        {
          name: '物料编码',
          value: 'materialCode',
          type: 'input'
        },
        {
          name: '申请表编号',
          value: 'disposalApplyCode',
          type: 'input'
        },
        {
          name: '调拨单编号',
          value: 'transferCode',
          type: 'input'
        },
        {
          name: '调拨日期',
          value: 'createTime',
          type: 'timeRangeFormat'
        },
        {
          name: '负责人',
          value: 'creatorName',
          type: 'input'
        },
        {
          name: '相关单位',
          value: 'transferOutOrgName',
          type: 'input'
        }
      ],
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          scopedSlots: { customRender: 'index' },
          width: 70
        },
        {
          title: '所属分公司',
          dataIndex: 'transferOutOrgComName',
          width: 150
        },
        {
          title: '调出单位',
          dataIndex: 'transferOutOrgName',
          width: 150
        },
        {
          title: '物资名称',
          dataIndex: 'materialName',
          width: 120
        },
        {
          title: '物料编码',
          dataIndex: 'materialCode',
          width: 120
        },
        {
          title: '规格型号',
          dataIndex: 'materialModel',
          width: 120
        },
        {
          title: '单位',
          dataIndex: 'materialUnit',
          width: 120
        },
        {
          title: '数量',
          dataIndex: 'actTransferSize',
          width: 120
        },
        {
          title: '单价（元）',
          dataIndex: 'estUnitPrice',
          width: 120
        },
        {
          title: '金额',
          dataIndex: 'amount',
          width: 120
        },
        {
          title: '申请表编号',
          dataIndex: 'disposalApplyCode',
          width: 120
        },
        {
          title: '调拨单编号',
          dataIndex: 'transferCode',
          width: 120
        },
        {
          title: '调拨日期',
          dataIndex: 'createTime',
          width: 120
        },
        {
          title: '负责人',
          dataIndex: 'creatorName',
          width: 120
        },
        {
          title: '备注',
          dataIndex: 'disposalReason',
          width: 120
        }
      ],
      selectedRowKeys: [],
      selectId: [],
      selectedRows: [],
      chooseData: 0,
      tableData: [],
      loadData: (params) => {
        let searchData = Object.assign(params, this.queryParam)
        if (searchData.createTime) {
          searchData.allocatDateStart = searchData.createTime[0]
          searchData.allocatDateEnd = searchData.createTime[1]
        }
        return transfersList(searchData).then((res) => {
          return res
        })
      },
      pathName: this.$route.path
    }
  },
  mounted: function () {
    //   this.list()
    //   this.$refs.exportSelect.tableToExcelFile(this.$refs.table)
    //   this.$refs.exportAll.tableToExcelFile(this.$refs.table)
    // console.log('mounted-----------table的Path', this.pathName)
    this.$refs.table.refresh()
    // 监听详情页返回刷新列表
    //this.pathName取的值是this.$route.path
    this.$bus.$on('handleRefresh' + this.pathName, () => {
      // console.log('--------------------收到了，刷新刷新刷新----------------',this.pathName)
      // this.list()
      this.$refs.table.refresh()
    })
  },
  beforeDestroy() {
    console.log('beforeDestroy-------table的Path', this.pathName)
    this.$bus.$off('handleRefresh' + this.pathName)
  },
  methods: {
    exportSelect: function () {
      if (this.selectedRowKeys.length > 0) {
        this.$refs.exportSelect.setDataAndGenerate(this.selectedRows)
      } else {
        this.$message.warning('请选择需要导出的数据')
      }
    },
    exportAll: function () {
      // 参数：查询条件、导出的回调
      if (this.totalSize) {
        this.pageNo = 1
        this.list(false, this.$refs.exportAll.setDataAndGenerate)
      } else {
        this.$message.warning('没有可导出的数据')
      }
    },
    list(params, callback) {
      this.loading = true
      let _this = this
      let searchData = Object.assign(
        {
          pageSize: callback ? _this.totalSize : _this.pageSize,
          pageNo: _this.pageNo
        },
        params,
        this.queryParam
      )
      if (searchData.createTime) {
        searchData.allocatDateStart = _this.formatDate(searchData.createTime[0])
        searchData.allocatDateEnd = _this.formatDate(searchData.createTime[1])
      }
      transfersList(searchData).then((res) => {
        if (res.code === 200) {
          if (callback) {
            if (res.data.length) {
              callback(res.data)
            } else {
              this.$message.warning('没有可导出的数据')
            }
            this.loading = false
            return
          }
          _this.tableData = res.data
          _this.totalSize = res.totalSize
        }
        this.loading = false
      })
    },
    search(val) {
      this.pageNo = 1
      this.queryParam = val
      // this.list(val)
      this.$refs.table.refresh()
    }
    // handlePageChange(pageNo, pageSize) {
    //     this.pageNo = pageNo
    //     this.list()
    //     this.selectedRowKeys = []
    // },
    // showSizeChange(pageNo, pageSize) {
    //     this.pageNo = 1
    //     this.pageSize = pageSize
    //     this.list()
    // },
    // onSelectChange(selectedRowKeys, selectedRows) {
    //     let _this = this
    //     _this.selectId = []
    //     selectedRows.map(item => {
    //         // 选中id
    //         _this.selectId.push(item.id)
    //     })
    //     this.chooseData = selectedRowKeys.length
    //     _this.selectedRowKeys = selectedRowKeys
    //     _this.selectedRows = selectedRows
    // }
  }
}
</script>
